package com.boost.socialNetwork.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.Window;

public class IdeaEntry extends LayoutPanel {

	private Label head;
	
	private final UserServiceAsync greetService = GWT
	.create(UserService.class);
	private TextBox IdeaTitle;
	private RichTextArea IdeaContent;
	
	public IdeaEntry() {
		greetService.getConnected(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
			Window.alert("Error");
				
			}

			@Override
			public void onSuccess(String result) {
				if(result==null)
				HistoryManager.newHistory("PageLogin");
				
			}
		});
		
		setHead(new Label("New Idea"));
		
		FlexTable IdeaTable = new FlexTable();
		add(IdeaTable);
		setWidgetLeftWidth(IdeaTable, 17.0, Unit.PX, 468.0, Unit.PX);
		setWidgetTopHeight(IdeaTable, 28.0, Unit.PX, 410.0, Unit.PX);
		
		FlexTable IdeaTitleTable = new FlexTable();
		IdeaTable.setWidget(0, 0, IdeaTitleTable);
		IdeaTitleTable.setHeight("100%");
		
		Label lblIdeaTitle = new Label("Idea title");
		lblIdeaTitle.setStyleName("gwt-label");
		IdeaTitleTable.setWidget(0, 0, lblIdeaTitle);
		lblIdeaTitle.setWidth("82px");
		
		IdeaTitle = new TextBox();
		IdeaTitleTable.setWidget(0, 1, IdeaTitle);
		IdeaTitle.setSize("236px", "21px");
		
		FlexTable IdeaContentTable = new FlexTable();
		IdeaTable.setWidget(1, 0, IdeaContentTable);
		
		Label lblIdea = new Label("Idea content:");
		lblIdea.setStyleName("gwt-label");
		IdeaContentTable.setWidget(0, 0, lblIdea);
		lblIdea.setWidth("81px");
		IdeaContentTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		
		IdeaContent = new RichTextArea();
		IdeaContentTable.setWidget(0, 1, IdeaContent);
		IdeaContent.setSize("347px", "284px");
		IdeaContentTable.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		
		FlexTable ButtonTable = new FlexTable();
		IdeaTable.setWidget(2, 0, ButtonTable);
		
		
		Button btnSubmit = new Button("Submit");
		
		btnSubmit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {				
				if(IdeaContent.getText().length()==0&&IdeaTitle.getText().length()==0)
					Window.alert("You cannot enter an empty idea.");
				else if(IdeaTitle.getText().length()==0 && IdeaContent.getText().length()!=0)
					Window.alert("You probably forgot to enter an idea title(Idea title cannot be empty).");
				else if(IdeaContent.getText().length()==0 && IdeaTitle.getText().length()!=0)
					Window.alert("You probably forgot to enter an idea title(Idea Content cannot be empty.)");
				else {
					greetService.getConnected(new AsyncCallback<String>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(String userKey) {
							greetService.createIdea( userKey, IdeaTitle.getText(), IdeaContent.getText(),
									new AsyncCallback<String>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub											
										}										

										@Override
										public void onSuccess(String result) {
											if (result.equals("OK")) {
												Window.alert("Create Successfully");
											//												RootPanel.get("post").clear();
//												RootPanel.get("head").clear();
//												Home home = new Home();
//												RootPanel.get("post").add(home);
//												RootPanel.get("head").add(home.getHead());											
												HistoryManager.newHistory("PageHome");
											} else {
												Window.alert("datastore closed");
											}
											
											
										}
									});
							
						}
						
					});
					
				}
			}
		});
		ButtonTable.setWidget(0, 0, btnSubmit);
		btnSubmit.setSize("56px", "30px");
		
		Button btnClearIdea = new Button("Clear Idea");
		btnClearIdea.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				IdeaTitle.setText("");
				IdeaContent.setText("");
			}
		});
		ButtonTable.setWidget(0, 1, btnClearIdea);
		btnClearIdea.setSize("84px", "30px");
		IdeaTable.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		IdeaTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	}

	public void setHead(Label head) {
		this.head = head;
	}

	public Label getHead() {
		return head;
	}
}
